//
//  SubscribeViewController.m
//  Tiger
//
//  Created by King on 11-10-19.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SubscribeViewController.h"

#define kEmail			@"email"
#define kMobile			@"mobile"
#define kReceived		@"received"
#define kErrorCode		@"errorCode"



@implementation SubscribeViewController

@synthesize subsButton;
@synthesize agreeButton;
@synthesize emailField;
@synthesize passwordField;
@synthesize activity;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	isRemember = YES;
	code = 1;
	if (isRemember) {
		UIImage *buttonImageNormal = [UIImage imageNamed:@"bn_fav.png"];
		[agreeButton setImage:buttonImageNormal forState:UIControlStateNormal];
	}
}

-(void)sendRequestPayInfo
{	
	long long phoneNo = [passwordField.text longLongValue];
	
	//NSLog(@"phoneNo = %qi",phoneNo);
	//NSLog(@"code = %d",code);
	//NSLog(@"passwordField.text = %@", passwordField.text);
	
	
	NSURL* serverURL = [NSURL URLWithString:URL_Subscribe];
	ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:serverURL];
	
	[request addPostValue:[emailField text] forKey:kEmail];
	[request addPostValue:[NSNumber numberWithLongLong:phoneNo] forKey:kMobile];
	[request addPostValue:[NSNumber numberWithInt:code] forKey:kReceived];
	
	[request setValidatesSecureCertificate:NO];
	[request setDelegate:self];
	[request startAsynchronous];
	[activity startAnimating];
	
}




- (void)keyboardDoneButtonTapped:(UIButton *)sender
{
	[passwordField resignFirstResponder];
	
}


- (IBAction)backButtonTapped:(id)sender{
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)agreeButtonTapped:(id)sender{
	if(isRemember == NO)
	{
		UIImage *buttonImageNormal = [UIImage imageNamed:@"bn_fav.png"];
		[agreeButton setImage:buttonImageNormal forState:UIControlStateNormal];
		isRemember = YES;
		code = 1;
	}
	else
	{
		UIImage *buttonImageNormal = [UIImage imageNamed:@"bt_fav.png"];
		[agreeButton setImage:buttonImageNormal forState:UIControlStateNormal];
		isRemember = NO;
		code = 0;
	}
}

- (IBAction)subsButtonTapped:(id)sender{

	[self sendRequestPayInfo];
	
}

#pragma mark UIResponder methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self.emailField resignFirstResponder];
	[self.passwordField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if (textField == self.emailField) {
		[self.passwordField becomeFirstResponder];
		return NO;
	}
	else if(textField == self.passwordField){
		[textField resignFirstResponder];
	}
	
	return YES;
}

#pragma mark ASIHTTPRequestDelegate methods
- (void)requestStarted:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	NSDictionary* dict = [[request responseString] JSONValue];
	//NSLog(@"dict : %@", dict);
	NSInteger status;
	status = [[dict objectForKey:kErrorCode] intValue];
	if (status == 0) 
	{
		UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"恭喜" message:@"订阅成功!" delegate:self 
											   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
		[alert addButtonWithTitle:@"确定"];
		[alert show];
	}
	if (status == -1) 
	{
		UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"Email为空!" delegate:self 
											   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
		[alert addButtonWithTitle:@"确定"];
		[alert show];
	}
	if (status == -2) 
	{
		UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"Email格式错误!" delegate:self 
											   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
		[alert addButtonWithTitle:@"确定"];
		[alert show];
		
	}
	if (status == -3) 
	{
		UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"没有输入手机号!" delegate:self 
											   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
		[alert addButtonWithTitle:@"确定"];
		[alert show];
		
	}
	if (status == -4) 
	{
		UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"手机号码格式错误！" delegate:self 
											   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
		[alert addButtonWithTitle:@"确定"];
		[alert show];
		
	}
	[activity stopAnimating];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"无可用的网络连接" delegate:self 
										   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
	[alert addButtonWithTitle:@"确定"];
	[alert show];
	[activity stopAnimating];
	
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

	self.subsButton = nil;
	self.agreeButton = nil;
	self.emailField = nil;
	self.passwordField = nil;
	self.activity = nil;

}

- (void)dealloc {
    [super dealloc];
	

	[subsButton release];
	[agreeButton release];
	
	[emailField release];
	[passwordField release];

	[activity release];
}


@end
